In part two of this article series, we will learn how we can create an AWS Lambda function and integrate it with Alexa skills. We will also learn how we can call an external API within our AWS Lambda function and process each intent. Let's get started.
Before you go through this article I recommend going through part one of this article here:
Log on to your AWS Console and click Services and search for "Lambda" function.

Now, on your AWS Lambda Console, click "create a function" and select Blueprints. Search for Alexa Skills in the search bar and select sdk-factskill and click the link. We need to work on the basic Alexa Skill template and change the existing code.

In the new tab created, you will be creating a new role for your Lambda function. Give a name to your role and click Allow.

Now, you will be able to see the role which you have created. So select the role you created and click Create Function. We will modify the Lambda Function code once the Lambda function is created.
Make sure you are in the Northern Virginia zone when you are creating. Alexa Skills kit trigger, as AWS Lambda function is available there.
Before we deep dive into the code let's integrate our Alexa Skill to this AWS Lambda function. Integration is done by configuring two things.
- Letting your AWS Lambda function exist from where this Lambda function is called. So we configure triggers in AWS Lambda function configuration.

- Configuring Alexa Skill to which Lambda function we have to call.

So we have to use two ids for integration - Skill Id and AWS Lambda ARN
Skill Id can be found in Alexa Console,
- In Alexa Console get skill ID by clicking view Skill ID and add the skill ID in Trigger configuration and click ok.
- AWS Lambda ARN is found in Lambda Console on top of your Lambda function.
Part 1 of Integration
Once Lambda function is created you have to add triggers to your AWS Lambda function. Select Alexa Skills Kit from the left menu, so now you are able to add a trigger to your AWS Lambda function.

Now click Configuration Required in the trigger and provide your Alexa Skill Id and click Add.

Part 2 of Integration
In Alexa Console, click "View Alexa Skill" and click "Endpoint" in the Left Menu to configure the AWS Lambda function.
Select AWS Lambda ARN and provide the AWS Lambda ARN of the lambda function you have created in the default region.

Now that Integration of Alexa Skills with lambda function is done, let us look at the lambda function code and modify the code to process our Alexa Skill request.
I have used WORDSAPI to get synonyms for the words. Create an account in WordsAPI to use it in Lambda function. Once you signup they will provide an X-Mashape-Key which we use for authorization of words API.
In Lambda function we use Alexa SDK and https node js modules.
- constAlexa = require('alexa-sdk');
- varhttp = require('https');
We have to create handlers for Alexa using Alexa SDK. In the handlers object, we have to write logic for each intent which we have created in our Alexa Skill. By default, we have code execution for Amazon-defined Intents like AMAZON, HelpIntent, etc.
- consthandlers = {
- 'LaunchRequest': function() {
- this.emit('GetNewFactIntent');
- },
- 'AMAZON.HelpIntent': function() {
- const speechOutput = 'You can ask me a word, I can help you with synonyms of the word';
- const reprompt = 'What can I help you with?';
- this.response.speak(speechOutput).listen(reprompt);
- this.emit(':responseReady');
- },
- 'AMAZON.CancelIntent': function() {
- this.response.speak(STOP_MESSAGE);
- this.emit(':responseReady');
- },
- 'AMAZON.StopIntent': function() {
- this.response.speak(STOP_MESSAGE);
- this.emit(':responseReady');
- },
- };
We do have some logic that gets executed for each Intent. Let us look at the logic for Help Intent.
- this.response.speak(speechOutput).listen(reprompt);
Here speaking the method within the response object will let Alexa Skills know that whatever is sent as a parameter has to be read to the end user.
The Listen method within the response object will make Alexa Skills wait for a response from the user for some time and keep the session active until the user responds.
We do also have a Launch Request Intent which gets invoked when the user says something like "Alexa, start Synonyms Finder", code within Launch the request intent gets executed. For LaunchRequest we have to maintain the session so we use the listen method.



Join the conversation! Your thoughts help the community grow.